package com.roubsite.site.action;

import com.roubsite.database.annotation.Dao;
import com.roubsite.site.dao.DemoTestDao;
import com.roubsite.site.dao.IDemoTestDao;
import com.roubsite.utils.JsonUtils;
import com.roubsite.utils.UuidUtils;
import com.roubsite.web.action.RSAction;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class DbAction extends RSAction {
    //使用注解的方式注入Dao接口，注解中的impl是dao接口的实现类，
    // dataSource是数据源，数据源在dataSource.properties中进行配置，一个dao接口可以注入不同的数据源实现多数据源操作
    @Dao(impl = DemoTestDao.class, dataSource = "dataSource1")
    IDemoTestDao demoDao;

    @Override
    public void execute() throws Exception {
        this.display("dbDemo.html");
    }

    public void doGet() throws IOException {
        this.print(JsonUtils.convertToString(demoDao.get()));
    }

    public void doDel() throws IOException {
        String id = getString($_P("id"));
        Map m = new HashMap();
        m.put("status", demoDao.del(id));
        this.print(JsonUtils.convertToString(m));
    }

    public void doUpdate() throws IOException {
        String id = getString($_P("id"));
        String name = getString($_P("name"));
        Map m = new HashMap();
        m.put("status", demoDao.update(id, name));
        this.print(JsonUtils.convertToString(m));
    }

    public void doInsert() throws IOException {
        String id = UuidUtils.getUuid();
        String name = getString($_P("name"));
        Map m = new HashMap();
        m.put("status", demoDao.insert(id, name));
        this.print(JsonUtils.convertToString(m));
    }


}
